home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / vector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  5.4 KB  |  230 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    vector.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include "vector.h"
  35.  
  36. #include "alloc.h"
  37. #include "collection.h"
  38. #include "error.h"
  39. #include "list.h"
  40. #include "number.h"
  41. #include "prim.h"
  42. #include "symbol.h"
  43.  
  44. /* primitives */
  45.  
  46. static Object vector_element_setter (Object vec, Object index, Object val);
  47. static Object vector_size (Object vec);
  48. static Object vector_append2 (Object vec1, Object vec2);
  49.  
  50. static struct primitive vector_prims[] =
  51. {
  52.     {"%vector-element", prim_3, vector_element},
  53.     {"%vector-element-setter", prim_3, vector_element_setter},
  54.     {"%vector-size", prim_1, vector_size},
  55.     {"%vector-append2", prim_2, vector_append2},
  56.     {"%vector", prim_1, make_sov},
  57. };
  58.  
  59. /* function definitions */
  60.  
  61. void
  62. init_vector_prims (void)
  63. {
  64.     int num;
  65.  
  66.     num = sizeof (vector_prims) / sizeof (struct primitive);
  67.  
  68.     init_prims (num, vector_prims);
  69. }
  70.  
  71. Object
  72. make_sov (Object el_list)
  73. {
  74.     Object obj, els;
  75.     int size, i;
  76.  
  77.     obj = allocate_object (sizeof (struct simple_object_vector));
  78.  
  79.     SOVTYPE (obj) = SimpleObjectVector;
  80.     size = 0;
  81.     els = el_list;
  82.     while (PAIRP (els)) {
  83.     size++;
  84.     els = CDR (els);
  85.     }
  86.     SOVSIZE (obj) = size;
  87.     SOVELS (obj) = (Object *) checking_malloc (size * sizeof (Object));
  88.  
  89.     els = el_list;
  90.     i = 0;
  91.     while (PAIRP (els)) {
  92.     SOVELS (obj)[i++] = CAR (els);
  93.     els = CDR (els);
  94.     }
  95.     return (obj);
  96. }
  97.  
  98. Object
  99. make_vector (int size, Object fill_obj)
  100. {
  101.     Object res;
  102.     int i;
  103.  
  104.     /* actually fabricate the vector */
  105.     res = allocate_object (sizeof (struct simple_object_vector));
  106.  
  107.     SOVTYPE (res) = SimpleObjectVector;
  108.     SOVSIZE (res) = size;
  109.     SOVELS (res) = (Object *) checking_malloc (size * sizeof (Object));
  110.  
  111.     for (i = 0; i < size; ++i) {
  112.     SOVELS (res)[i] = fill_obj;
  113.     }
  114.     return (res);
  115. }
  116.  
  117. /* Called with args to make */
  118. Object
  119. make_vector_driver (Object args)
  120. {
  121.     int size, i;
  122.     Object size_obj, fill_obj, res;
  123.  
  124.     size = 0;
  125.     size_obj = NULL;
  126.     fill_obj = NULL;
  127.  
  128.     while (!NULLP (args)) {
  129.     if (FIRST (args) == size_keyword) {
  130.         size_obj = SECOND (args);
  131.     } else if (FIRST (args) == fill_keyword) {
  132.         fill_obj = SECOND (args);
  133.     } else {
  134.         error ("make: unsupported keyword for <vector> class", FIRST (args), NULL);
  135.     }
  136.     args = CDR (CDR (args));
  137.     }
  138.     if (size_obj) {
  139.     if (!INTEGERP (size_obj)) {
  140.         error ("make: value of size: argument must be an integer", size_obj, NULL);
  141.     }
  142.     size = INTVAL (size_obj);
  143.     }
  144.     if (!fill_obj) {
  145.     fill_obj = false_object;
  146.     }
  147.     return make_vector (size, fill_obj);
  148. }
  149.  
  150. Object
  151. vector_to_list (Object vec)
  152. {
  153.     int i;
  154.     Object first, cur, acons;
  155.  
  156.     cur = make_empty_list ();
  157.     for (i = 0; i < SOVSIZE (vec); ++i) {
  158.     acons = cons (SOVELS (vec)[i], make_empty_list ());
  159.     if (!NULLP (cur)) {
  160.         CDR (cur) = acons;
  161.     } else {
  162.         first = acons;
  163.     }
  164.     cur = acons;
  165.     }
  166.     return (first);
  167. }
  168.  
  169. /* primitives */
  170.  
  171. Object
  172. vector_element (Object vec, Object index, Object default_ob)
  173. {
  174.     int i, size;
  175.  
  176.     i = INTVAL (index);
  177.     size = SOVSIZE (vec);
  178.     if ((i < 0) || (i >= size)) {
  179.     if (default_ob == default_object) {
  180.         error ("element: index out of range", vec, index, NULL);
  181.     } else {
  182.         return default_ob;
  183.     }
  184.     }
  185.     return (SOVELS (vec)[i]);
  186. }
  187.  
  188. static Object
  189. vector_element_setter (Object vec, Object index, Object val)
  190. {
  191.     int i, size;
  192.  
  193.     i = INTVAL (index);
  194.     size = SOVSIZE (vec);
  195.     if ((i < 0) || (i >= size)) {
  196.     error ("element-setter: index out of range", vec, index, NULL);
  197.     }
  198.     return (SOVELS (vec)[i] = val);
  199. }
  200.  
  201. static Object
  202. vector_size (Object vec)
  203. {
  204.     return (make_integer (SOVSIZE (vec)));
  205. }
  206.  
  207. static Object
  208. vector_append2 (Object vec1, Object vec2)
  209. {
  210.     Object new_vec;
  211.     int new_size, i, size1;
  212.  
  213.     size1 = SOVSIZE (vec1);
  214.     new_size = size1 + SOVSIZE (vec2);
  215.     new_vec = allocate_object (sizeof (struct simple_object_vector));
  216.  
  217.     SOVTYPE (new_vec) = SimpleObjectVector;
  218.     SOVSIZE (new_vec) = new_size;
  219.     SOVELS (new_vec) = (Object *) checking_malloc (new_size * sizeof (Object));
  220.  
  221.     for (i = 0; i < new_size; ++i) {
  222.     if (i < size1) {
  223.         SOVELS (new_vec)[i] = SOVELS (vec1)[i];
  224.     } else {
  225.         SOVELS (new_vec)[i] = SOVELS (vec2)[i - size1];
  226.     }
  227.     }
  228.     return (new_vec);
  229. }
  230.